package com.jckl.foodathon.common.utils;

import java.io.ByteArrayOutputStream;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;

public class BitmapEncode {
	
	public static String bitmapToEncodedString (Bitmap bm){
		if(bm != null){
			ByteArrayOutputStream baos = new ByteArrayOutputStream();  
			bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object   
			byte[] b = baos.toByteArray(); 
			String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
			
			return encodedImage;
		}else{
			return null;
		}

	}
	
	public static Bitmap encodedStringToBitmap (String str){
		if(str != null){
			byte[] b = str.getBytes();
			Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
			
			return bitmap;
		}else{
			return null;
		}
		
	}
	
	public static byte[] bitmapToByte (Bitmap bm){
		if(bm != null){
			ByteArrayOutputStream baos = new ByteArrayOutputStream();  
			bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object   
			byte[] b = baos.toByteArray(); 
			
			return b;
		}else{
			return null;
		}
	}
	
	public static Bitmap byteToBitmap (byte[] b){
		if(b != null){
			Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
			
			return bitmap;
		}else{
			return null;
		}
		
	}
	
	

}
